package com.paranoidvulture.javaEngine.Utilities;

import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;

import com.paranoidvulture.javaEngine.MainPackage.ScreenManager;

/* Camera2D
 * This is the basic camera that will be used for game play
 * It is used by subtracting the object's position you are drawing from
 *  the camera's current position. This gives you a, World Position, almost.
 */
public class Camera2D {
	private float posX, posY, virtualPosX, virtualPosY;
	private float speed = 3;
	private int camWidth, camHeight, virtualWidth, virtualHeight;
	private ScreenManager screenManager;
	private Rectangle2D camRec, virtualRec;
	
	public void ScrollUp() { 
		if (!virtualRec.isEmpty()) {
			virtualPosY -= speed;
			virtualHeight -= speed;
			virtualRec.setRect(virtualPosX, virtualPosY, virtualWidth, virtualHeight);
		} else {
			posY -= speed; 
		}
	}
    public void ScrollDown() { 
    	if (!virtualRec.isEmpty()) {
			virtualPosY += speed;
			virtualHeight += speed;
			virtualRec.setRect(virtualPosX, virtualPosY, virtualWidth, virtualHeight);
		} else {
			posY += speed; 
		}
	}
	public void ScrollRight() { 
    	if (!virtualRec.isEmpty()) {
			virtualPosX += speed;
			virtualWidth += speed;
			virtualRec.setRect(virtualPosX, virtualPosY, virtualWidth, virtualHeight);
			System.out.println(virtualPosX + ", " + virtualWidth + ", True " + posX + ", " + camWidth);
		} else {
			posX += speed;
		}
	}
    public void ScrollLeft() { 
    	if (!virtualRec.isEmpty()) {
			virtualPosX -= speed;
			virtualWidth -= speed;
			virtualRec.setRect(virtualPosX, virtualPosY, virtualWidth, virtualHeight);
		} else {
			posX -= speed; 
		}
	}
    public float getVirtualPosX() {
		return virtualPosX;
	}
	public void setVirtualPosX(float virtualPosX) {
		this.virtualPosX = virtualPosX;
	}
	public float getVirtualPosY() {
		return virtualPosY;
	}
	public void setVirtualPosY(float virtualPosY) {
		this.virtualPosY = virtualPosY;
	}
	public Rectangle2D getVirtualRec() {
		return virtualRec;
	}
	public void setVirtualRec(Rectangle2D virtualRec) {
		this.virtualRec = virtualRec;
	}

    
    public Rectangle2D getCamRec() {
		return camRec;
	}
	
    public void setCamRec(Rectangle2D camRec) {
		this.camRec = camRec;
	}
	
	public float getPosX(){
    	return posX;
    }
    
    public void setPosX(float x){
    	this.posX = x;
    }
    
    public float getPosY(){
    	return posY;
    }
    
    public void setPosY(float y){
    	this.posY = y;
    }
    
    public float getSpeed(){
    	return speed;
    }
    
	public void setSpeed(float s){
		this.speed = s;
	}
	
	public void setCameraWidth(int newWidth) {
		camWidth = newWidth;
	}
	
	public void setCameraHeight(int newHeight) {
		camHeight = newHeight;
	}
	
	public int getCameraWidth() {
		return camWidth;
	}
	
	public int getCameraHeight() {
		return camHeight;
	}
	
	public void setVirtualWidth(int newWidth) {
		virtualWidth = newWidth;
	}
	
	public void setVirtualHeight(int newHeight) {
		virtualHeight = newHeight;
	}
	
	public int getVirtualWidth() {
		return virtualWidth;
	}
	
	public int getVirtualHeight() {
		return virtualHeight;
	}
	
	public Camera2D(ScreenManager s){
		this.screenManager = s;
		this.camHeight = s.getHeight();
		this.camWidth = s.getWidth();
		this.virtualRec = new Rectangle2D.Double();
	}
	
	public Camera2D(ScreenManager s, int w, int h){
		this.screenManager = s;
		this.camHeight = h;
		this.camWidth = w;
		this.virtualWidth = w;
		this.virtualHeight = h;
		this.camRec = new Rectangle2D.Double(this.getPosX(), this.getPosY(), w, h);
		this.virtualRec = new Rectangle2D.Double(this.getPosX(), this.getPosY(), w, h);
	}
	
	public void LockCamera(int tileWidth, int tileHeight, int[][] map){
		this.posX = restrictMovement(tileWidth, tileHeight, map, 'x', this.posX);
		this.posY = restrictMovement(tileWidth, tileHeight, map, 'y', this.posY);
	}
	
	public void LockCamera(int tileWidth, int tileHeight, int mapWidth, int mapHeight) {
		this.virtualPosX = restrictMovement(tileWidth, tileHeight, mapWidth,mapHeight, 'x', this.virtualPosX);
		this.virtualPosY = restrictMovement(tileWidth, tileHeight, mapWidth,mapHeight, 'y', this.virtualPosY);
		this.virtualWidth = (int) (this.camWidth + this.virtualPosX);
		this.virtualHeight = (int) (this.camHeight + this.virtualPosY);
	}
	
	private int restrictMovement(int tileWidth, int tileHeight, int[][] map, char x, float input) {
		int value = 0;
		
		if (x == 'x'){
			if (input <= 0){
					return 0;
				}
			
			else if (input >= (map[0].length * tileWidth) - this.getCameraWidth()) { 
				value = (int)((map[0].length * tileWidth) - this.getCameraWidth());
				return value;
			}
			else return (int)input;
		}
		
		else if (x == 'y'){
			if (input <= 0){ return 0;}
			
			else if (input >= (map.length * tileHeight) - this.getCameraHeight()) { 
				value = (int)((map.length * tileWidth) - this.getCameraHeight());
				return value;
			}
			else return (int)input;
		}
		return (int)input;
		
	}
	
	private int restrictMovement(int tileWidth, int tileHeight, int mapWidth, int mapHeight, char x, float input) {
		int value = 0;
		
		if (x == 'x'){
			if (input <= 0){
					return 0;
				}
			
			else if (input >= (mapWidth * tileWidth) - this.getCameraWidth()) { 
				value = (int)((mapWidth * tileWidth) - this.getCameraWidth());
				return value;
			}
			else return (int)input;
		}
		
		else if (x == 'y'){
			if (input <= 0){ return 0;}
			
			else if (input >= (mapHeight * tileHeight) - this.getCameraHeight()) { 
				value = (int)((mapHeight * tileWidth) - this.getCameraHeight());
				return value;
			}
			else return (int)input;
		}
		return (int)input;
	}
}
